package org.to_zero.ssm.sdm.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.to_zero.ssm.sdm.entity.User;
import org.to_zero.ssm.sdm.entity.vo.ResponseEntity;
import org.to_zero.ssm.sdm.mapper.UserMapper;
import org.to_zero.ssm.sdm.service.AdminService;
import org.to_zero.ssm.sdm.util.RedisUtil;
import org.to_zero.ssm.sdm.util.SysUtil;
import org.to_zero.ssm.sdm.util.Toolkit;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UserMapper userMapper;

    private RedisUtil<User> redisUtil = new RedisUtil<>(User.class);

    @Override
    public ResponseEntity login(String username, String password, String verifyCode, boolean isRememberMe) {
        ResponseEntity responseEntity = ResponseEntity.error("验证码输入错误，请尝试重新输入");
        String storedVerifyCode = redisUtil.getString(SysUtil.VERIFY_CODE);
        // 判断验证码是否一致
        if (verifyCode.equalsIgnoreCase(storedVerifyCode)) {
            responseEntity = ResponseEntity.error("用户名或密码错误，请重新尝试！");
            User user = userMapper.login(username, password);
            //是否存在用户
            if (Objects.nonNull(user)) {
                //判断是否勾选记住我
                if (isRememberMe) {
                    User rememberUser = new User();
                    rememberUser.setCode(username);
                    rememberUser.setPassword(password);
                    redisUtil.put(SysUtil.REMEMBER_TOKEN, rememberUser);
                } else {
                    redisUtil.del(SysUtil.REMEMBER_TOKEN);
                }
                //生成uuid，存储用户信息
                String uuidToken = Toolkit.getUUIDToken();
                redisUtil.put(uuidToken, user);
                Map<String, String> map = new HashMap<>();
                map.put(SysUtil.LOGIN_TOKEN, uuidToken);
                responseEntity = ResponseEntity.success(map);
            }
        }
        return responseEntity;
    }

    @Override
    public ResponseEntity rememberMe() {
        User user = redisUtil.get(SysUtil.REMEMBER_TOKEN);
        return Objects.nonNull(user) ? ResponseEntity.success(user) : ResponseEntity.error();
    }

    @Override
    public ResponseEntity register(User user) {
        int flg = userMapper.register(user);
        if (flg != 0) {
            return ResponseEntity.error("此账号已被注册，请重新尝试！");
        }
        user.setRoleCode("student");
        flg = userMapper.insertSelective(user);
        return flg > 0 ? ResponseEntity.success("注册成功") : ResponseEntity.error("出了一点问题o(╥﹏╥)o");
    }

    @Override
    public ResponseEntity logout(String token) {
        redisUtil.del(token);
        redisUtil.del(SysUtil.REMEMBER_TOKEN);
        return ResponseEntity.success();
    }

    @Override
    public ResponseEntity getAdmin(String token) {
        User loginUser = redisUtil.get(token);
        return Objects.nonNull(loginUser) ? ResponseEntity.success(loginUser) : ResponseEntity.error();
    }

    @Override
    public ResponseEntity changePassword(Integer id, String currentPassword, String password) {
        return userMapper.changePassword(id, currentPassword, password) > 0 ? ResponseEntity.success("密码修改成功") : ResponseEntity.error("当前密码输入有误");
    }

    @Override
    @Transactional
    public ResponseEntity changeProfile(User user, HttpServletRequest request) {
        int flg = userMapper.updateByPrimaryKeySelective(user);
        if (flg > 0) {
            String token = Toolkit.getToken(request);
            User updatedUser = userMapper.selectByPrimaryKey(user.getId());
            redisUtil.put(token, updatedUser);
        }
        return flg > 0 ? ResponseEntity.success("资料修改成功") : ResponseEntity.error("出了一些问题/(ㄒoㄒ)/~~");
    }
}
